package com.example.im.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.im.IMApplication;

/**
 * 保存
 * 获取数据
 */
public class SpUtils {
    public static final String IS_NEW_INVITE = "is_new_invite";//新的邀请标记
    private static SpUtils instance = new SpUtils();
    private static SharedPreferences im;

    private SpUtils(){}

    //单例
    public static SpUtils getInstance(){
        //这样写的好处是优化内存，使其生命周期相同，避免耗时操作出bug
        if (im == null){
            im = IMApplication.getGlobalApplication().getSharedPreferences("im", Context.MODE_PRIVATE);
        }

        return instance;
    }
    //保存
    public void save(String key,Object value){
        if (value instanceof String){
            im.edit().putString(key, (String) value).apply();
        }else if (value instanceof Boolean){
            im.edit().putBoolean(key, (Boolean) value).apply();

        }else if (value instanceof  Integer){
            im.edit().putInt(key, (Integer) value).apply();
        }


    }
    //获取数据的方法
    public String getString(String key,String defValue){
        return im.getString(key,defValue);
    }

    //获取boolean数据
    public boolean getBoolean(String key,boolean defValue){
        return im.getBoolean(key,defValue);
    }
    //获取int类型数据
    public int getInt(String key,int defValue){
        return im.getInt(key, defValue);
    }
}
